
from read_list_from_file  import read_price_list_Phone as read_price_list
import numpy as np
import pylab as pl
import datetime

if __name__ == '__main__':
    readinfo=read_price_list.Get_Price_List()
    list=[]
    list=readinfo.get_infolist()
    times=[]
    for number in range(0,len(list))  :
        ifhas=0
        for x in range(0,len(times)):
            if(list[number]['time'].split(' ')[0]==times[x]):
                ifhas=1
                break
        if(ifhas==0):
            times.append(list[number]['time'].split(' ')[0])
    for  i in range(0,len(times)-1):
        for j in range(0,len(times)-1):
            if(times[j]>times[j+1]):
                x=times[j]
                times[j]=times[j+1]
                times[j+1]=x
                
    
    price = ('0-200', '200-600',  '600-?')
    
    
    numbers_1=[]
    totalprice_1=[]
    numbers_2=[]
    totalprice_2=[]
    numbers_3=[]
    totalprice_3=[]
    
    for i in range(0,len(times)):
        numbers_1.append(0)
        totalprice_1.append(0)
        numbers_2.append(0)
        totalprice_2.append(0)
        numbers_3.append(0)
        totalprice_3.append(0)
    
    for number in range(0,len(list))  :
        if(list[number]['price']<=200):
            for T in range(0,len(times)):
               if(list[number]['time'].split(' ')[0]==times[T]):
                   numbers_1[T]=numbers_1[T]+1
                   totalprice_1[T]=totalprice_1[T]+list[number]['price']
    averagePrice_1=[]
    for i in range(0,len(numbers_1)):
       if(numbers_1[i]!=0):
         averagePrice_1.append(totalprice_1[i]/numbers_1[i])
       else :
         averagePrice_1.append(0)
         print(totalprice_1[i])
    print(averagePrice_1)
    
    for number in range(0,len(list))  :
        if(list[number]['price']>200 and list[number]['price']<=600 ):
            for T in range(0,len(times)):
               if(list[number]['time'].split(' ')[0]==times[T]):
                   numbers_2[T]=numbers_2[T]+1
                   totalprice_2[T]=totalprice_2[T]+list[number]['price']
    averagePrice_2=[]
    for i in range(0,len(numbers_2)):
       if(numbers_2[i]!=0):
         averagePrice_2.append(totalprice_2[i]/numbers_2[i])
       else :
         averagePrice_2.append(0)
    print(averagePrice_2)

    for number in range(0,len(list))  :
        if(list[number]['price']>600  ):
            for T in range(0,len(times)):
               if(list[number]['time'].split(' ')[0]==times[T]):
                   numbers_3[T]=numbers_3[T]+1
                   totalprice_3[T]=totalprice_3[T]+list[number]['price']
    averagePrice_3=[]
    for i in range(0,len(numbers_3)):
       if(numbers_3[i]!=0):
         averagePrice_3.append(totalprice_3[i]/numbers_3[i])
       else :
         averagePrice_3.append(0)
    print(averagePrice_3)
    
    print(times)
    timestoString=[]
    for x in range(0,len(times)):
        times_list = str(times[x]).split('-')
        year = int(times_list[0])
        month=int(times_list[1])
        day=int(times_list[2])
        timestoString.append(datetime.datetime(year,month,day))

    pl.plot(timestoString, averagePrice_1, 'o--')
    pl.plot(timestoString, averagePrice_2, 'o--')
    pl.plot(timestoString, averagePrice_3, 'o--')
    pl.gcf().autofmt_xdate()   
    pl.title('Trend of Price')
    pl.xlabel('time')
    pl.ylabel('average_price')
    pl.show()
    